package tests;

import columnar.Columnarfile;
import global.AttrType;
import global.Size;
import global.SystemDefs;
import global.TupleOrder;
import heap.Tuple;
import iterator.ColumnarDuplElim;
import iterator.ColumnarSort;

public class sorttest {
	public static void main(String argv[])
	{
		AttrType[] type = new AttrType[4];
			type[0] = new AttrType(AttrType.attrString);
			type[1] = new AttrType(AttrType.attrString);
			type[2] = new AttrType(AttrType.attrInteger);
			type[3] = new AttrType(AttrType.attrInteger);
			SystemDefs sysdef = new SystemDefs("db10",100000,100,"Clock");
			Size.STRINGSIZE=25;
		String[] colnames = {"A","B","C","D"};
		String ColumnarFilename="colfile1";
		Columnarfile cf = new Columnarfile ("colfile1",4,type);
		cf.setColumnNames(colnames);
		cf.setColumnarFileInfo(Size.STRINGSIZE);
		Tuple record = new Tuple();
		short[] fldOff = {0,(short)Size.STRINGSIZE,(short)(2*Size.STRINGSIZE),(short)(2*Size.STRINGSIZE+4)};
		TupleOrder sort_order = new TupleOrder(TupleOrder.Ascending);
		int  strCount =0;
		short[] str_sizes = new short[cf.getnumColumns()];
		for (AttrType attr: type)
		{
			if (attr.attrType == AttrType.attrString)
			{
				
				str_sizes[strCount] =(short) Size.STRINGSIZE;
			}
			if (attr.attrType == AttrType.attrInteger)
			{
				str_sizes[strCount] = 0;
			}
			strCount++;
		}
		System.out.println("i am before sort");
		try{
		ColumnarSort cs = new ColumnarSort(type, (short)cf.getnumColumns(), str_sizes, cf, 4, sort_order, 4, 70);
		record.setTupleMetaData(58, (short)4, fldOff);
		/*for (i=0;i<Integer.parseInt(argv[3]);i++)
			tid.recordIDs[i] = new RID();*/
		int i = 0;
		while((record = cs.get_next()) != null)
		{
			//Tuple data = record.getData();
			record.setTupleMetaData(58, (short)4, fldOff);
			System.out.print("Record:"+i++);
			record.print(type);
		}
		//tscan.closetuplescan();
		cs.close();
		System.out.println("i am before elim");
		ColumnarDuplElim cd = new ColumnarDuplElim(type, (short)cf.getnumColumns(), str_sizes, ColumnarFilename, 10, false);
		record = new Tuple();
		record.setTupleMetaData(58, (short)4, fldOff);
		while((record = cd.get_next()) != null)
		{
			//Tuple data = record.getData();
			record.setTupleMetaData(58, (short)4, fldOff);
			System.out.print("DUP Record:"+i++);
			record.print(type);
		}
		cd.close();
		SystemDefs.JavabaseBM.resetAllPinCount();
		SystemDefs.JavabaseBM.flushAllPages();
		SystemDefs.JavabaseDB.closeDB();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}